package com.gitbl.webchat.roomnode;

import com.gitbl.webchat.message.RoomMessage;
import com.gitbl.webchat.message.RoomsNodeMessage;
import com.sun.management.OperatingSystemMXBean;


import java.lang.management.ManagementFactory;
import java.util.Map;
import java.util.TreeMap;

//节点信息
public class NodeDetails {
    static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

    //节点开放地址
    public static String addr="127.0.0.1:8081";

    public NodeDetails(){
        node = new RoomsNodeMessage();
        node.setCpu(cpuLoad());
        node.setMemory(memoryLoad());
        node.setAddress("ws://"+addr+"/room/");
        node.setRoomCount(0);
        node.setTotalMember(0);
        node.setApiAddress("http://"+addr+"/create");
        rooms = new TreeMap<>();
    }
    public RoomsNodeMessage node;
    public Map<String, RoomMessage> rooms;



    int cpuLoad() {
        double cpuLoad = osmxb.getSystemCpuLoad();
        int percentCpuLoad = (int) (cpuLoad * 100);
        return percentCpuLoad;

    }
    int memoryLoad() {
        double totalvirtualMemory = osmxb.getTotalPhysicalMemorySize();
        double freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();

        double value = freePhysicalMemorySize / totalvirtualMemory;
        int percentMemoryLoad = (int) ((1 - value) * 100);
        return percentMemoryLoad;
    }
}
